package com.stedu.service.impl;

import com.stedu.bean.Cars;
import com.stedu.exception.SteduException;
import com.stedu.mapper.CarsMapper;
import com.stedu.service.CarsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CarsServiceImpl implements CarsService {
    @Autowired
    private CarsMapper carsMapper;
    @Override
    public boolean insert(Cars c) throws SteduException {
        //判断添加后的车牌号不能与其他车牌号相同
        if(carsMapper.selectCplate(c.getCplate())!=null )
        {
            //抛出自定义异常
            throw new SteduException("该车牌号已经存在，无法修改");
        }
        c.setCstate("2");

        return carsMapper.insert(c)==1 ;
    }

    @Override
    public boolean delete(Integer cid) throws SteduException {
        //判断该车是否被出租中出租中的车无法删除有订单信息的车无法删除
        Cars c = carsMapper.selectBycid(cid);
       int a= Integer.parseInt(c.getCstate());
        if(a == 1){
            throw new SteduException("该车正在出租，无法删除");
        } else{
            return carsMapper.delete(cid)==1;
        }
    }

    @Override
    public boolean update(Cars c) throws SteduException {
        //判断修改后的车牌号不能与其他车牌号相同
        if (carsMapper.selectCplate(c.getCplate())!=null)
        {
            //抛出自定义异常
            throw new SteduException("该车牌号已经存在，无法重复");
        }
     return carsMapper.update(c)==1;
    }

    @Override
    public boolean updateCstate(String cstate, Integer cid) {
        return carsMapper.updateCstate(cstate, cid)==1;
    }

    @Override
    public Cars selectBycid(Integer cid) {
        return carsMapper.selectBycid(cid);
    }

    @Override
    public String selectCplate(String cplate) {
        return carsMapper.selectCplate(cplate);
    }

    @Override
    public List<Cars> selectBycplate(String cplate,String cstate) {
        return carsMapper.selectBycplate(cplate,cstate);
    }
}
